package org.jeecg.modules.icms.invoice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Sheet;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.icms.customs.entity.IcmsCustomsDetail;
import org.jeecg.modules.icms.invoice.dto.IcmsInvoiceDetailDto;
import org.jeecg.modules.icms.invoice.entity.IcmsInvoiceDetail;
import org.jeecg.modules.icms.invoice.entity.IcmsInvoiceHeader;
import org.jeecg.modules.icms.invoice.mapper.IcmsInvoiceDetailMapper;
import org.jeecg.modules.icms.invoice.service.IIcmsInvoiceDetailService;
import org.jeecg.modules.icms.invoice.vo.IcmsInvoiceDetailVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 发票行表
 * @Author: jeecg-boot
 * @Date:   2024-01-10
 * @Version: V1.0
 */
@Service
public class IcmsInvoiceDetailServiceImpl extends ServiceImpl<IcmsInvoiceDetailMapper, IcmsInvoiceDetail> implements IIcmsInvoiceDetailService {

    @Resource
    private IcmsInvoiceDetailMapper icmsInvoiceDetailMapper;

    @Override
    public void addDetailInfo(Sheet sheet, List<IcmsInvoiceDetailDto> icmsInvoiceDetailDto, List<Integer> ljmcRows, List<IcmsInvoiceHeader> icmsInvoiceHeaderList) {

        //处理detail
        ljmcRows.add(0,-1);
        ljmcRows.add(icmsInvoiceDetailDto.size()+1);
        for( int i=0;i<ljmcRows.size()-1 ;i++){
            for(int j = ljmcRows.get(i)+1;j < ljmcRows.get(i+1)-1;j++){
                IcmsInvoiceDetail icmsInvoiceDetail = new IcmsInvoiceDetail();
                icmsInvoiceDetail.setPartName(icmsInvoiceDetailDto.get(j).getPartName());
                icmsInvoiceDetail.setPartNo(icmsInvoiceDetailDto.get(j).getPartNo());
                icmsInvoiceDetail.setUnit(icmsInvoiceDetailDto.get(j).getUnit());
                icmsInvoiceDetail.setPartNameEn(icmsInvoiceDetailDto.get(j).getPartNameEn());
                icmsInvoiceDetail.setAccountNature(icmsInvoiceDetailDto.get(j).getAccountNature());
                icmsInvoiceDetail.setRemark(icmsInvoiceDetailDto.get(j).getRemark());

                try{
                    if(icmsInvoiceDetailDto.get(j).getQty() != null)
                        icmsInvoiceDetail.setQty(Double.parseDouble(icmsInvoiceDetailDto.get(j).getQty()));
                    if(icmsInvoiceDetailDto.get(j).getKphsdj() != null)
                        icmsInvoiceDetail.setKphsdj(Double.parseDouble(icmsInvoiceDetailDto.get(j).getKphsdj()));
                    if(icmsInvoiceDetailDto.get(j).getWsdj() != null)
                        icmsInvoiceDetail.setWsdj(Double.parseDouble(icmsInvoiceDetailDto.get(j).getWsdj()));
                    if(icmsInvoiceDetailDto.get(j).getTaxRate() != null)
                        icmsInvoiceDetail.setTaxRate(Double.parseDouble(icmsInvoiceDetailDto.get(j).getTaxRate()));
                    if(icmsInvoiceDetailDto.get(j).getTaxAmount() != null)
                        icmsInvoiceDetail.setTaxAmount(Double.parseDouble(icmsInvoiceDetailDto.get(j).getTaxAmount()));
                    if(icmsInvoiceDetailDto.get(j).getJshj() != null)
                        icmsInvoiceDetail.setJshj(Double.parseDouble(icmsInvoiceDetailDto.get(j).getJshj()));
                    if(icmsInvoiceDetailDto.get(j).getWsje() != null)
                        icmsInvoiceDetail.setWsje(Double.parseDouble(icmsInvoiceDetailDto.get(j).getWsje()));
                } catch(NumberFormatException e){
                    throw new NumberFormatException(sheet.getSheetName()+"表中：数量或金额必须为数字！请检查数据格式!");
                }
                icmsInvoiceDetail.setInvoiceRemark(icmsInvoiceDetailDto.get(j).getInvoiceRemark());
                if(icmsInvoiceDetailDto.get(j).getCustomsFlag() != null){
                    if(!"0".equals(icmsInvoiceDetailDto.get(j).getCustomsFlag()) && !"1".equals(icmsInvoiceDetailDto.get(j).getCustomsFlag())){
                        throw new JeecgBootException(sheet.getSheetName()+"表中:\"是否报关\"列请按照模板填写！");
                    }
                }
                icmsInvoiceDetail.setCustomsFlag(icmsInvoiceDetailDto.get(j).getCustomsFlag());
                icmsInvoiceDetail.setInvoiceHeaderId(icmsInvoiceHeaderList.get(i).getId());
                icmsInvoiceDetailMapper.insert(icmsInvoiceDetail);
            }
        }
    }

    @Override
    public IPage<IcmsInvoiceDetailVo> queryAll(IcmsInvoiceDetailVo icmsInvoiceDetailVo, Integer pageNo, Integer pageSize, HttpServletRequest req){
        Page<IcmsInvoiceDetailVo> page = new Page<IcmsInvoiceDetailVo>(pageNo, pageSize);
        return icmsInvoiceDetailMapper.queryAll(icmsInvoiceDetailVo,page);
    }

    @Override
    public Map<String, IcmsInvoiceDetail> queryInvoiceDetailInfoByContract(String contractNo) {
        List<IcmsInvoiceDetail> detailList = icmsInvoiceDetailMapper.queryInvoiceDetailInfoByContract(contractNo);
        Map<String, IcmsInvoiceDetail> map = detailList.stream().collect(Collectors.toMap(
                IcmsInvoiceDetail::getPartNo,
                IcmsInvoiceDetail -> IcmsInvoiceDetail
        ));
        return map;
    }
}
